package com.liuxin.mycontactdemo.activity;

import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import com.liuxin.mycontactdemo.R;
import com.liuxin.mycontactdemo.dbhelper.ContactOpenHelper;
import com.liuxin.mycontactdemo.provider.ContactProvider;
import com.liuxin.mycontactdemo.untils.ThreadUtils;
import com.liuxin.mycontactdemo.untils.ToastUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class EditContactActivity extends AppCompatActivity {

    @InjectView(R.id.editNameEt)
    EditText editNameEt;
    @InjectView(R.id.editPhoneEt)
    EditText editPhoneEt;
    @InjectView(R.id.editEmailEt)
    EditText editEmailEt;
    @InjectView(R.id.editQQEt)
    EditText editQQEt;
    @InjectView(R.id.editBtn)
    Button editBtn;

    private Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_contact);
        ButterKnife.inject(this);
        intent=getIntent();
        editNameEt.setText(intent.getStringExtra("name"));
        editPhoneEt.setText(intent.getStringExtra("phone"));
        editEmailEt.setText(intent.getStringExtra("email"));
        editQQEt.setText(intent.getStringExtra("qq"));

    }

    @OnClick(R.id.editBtn)
    public void onViewClicked() {
        ThreadUtils.runInUIThread(new Runnable() {
            @Override
            public void run() {
                ContentValues cv=new ContentValues();
                cv.put(ContactOpenHelper.ContactTable.NAME,editNameEt.getText().toString());
                cv.put(ContactOpenHelper.ContactTable.PHONE,editPhoneEt.getText().toString());
                cv.put(ContactOpenHelper.ContactTable.EMAIL,editEmailEt.getText().toString());
                cv.put(ContactOpenHelper.ContactTable.QQ,editQQEt.getText().toString());
                getContentResolver().update(ContactProvider.URI_CONTACT,cv,"name=?",new String[]{intent.getStringExtra("name")});
                ToastUtils.showToastSafe(EditContactActivity.this,"联系人已修改！！！");
                finish();
            }
        });
    }
}
